package com.wanlian.candy.controller;

import com.wanlian.common.PageCondition;
import com.wanlian.entity.Config;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.ConfigServices;
import com.wanlian.vo.VersionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/config")
public class ConfigController {


    @Autowired
    public ConfigServices configServices;


    /**
     * 添加配置信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.doAdd(config);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 添加版本更新信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add/version", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addVersion(@RequestBody String data) {
        VersionVo versionVo = JsonStringUtil.readValue(data, VersionVo.class);
        ResultModel resultModel = configServices.doAdd(versionVo);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 禁用配置信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/disable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String disable(@RequestBody String data, HttpSession session) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.doeditDisable(config);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 启用配置信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String enable(@RequestBody String data, HttpSession session) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.doeditEnable(config);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查看配置信息列表
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = configServices.queryConfigList(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按id查询配置
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/getconfig", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listById(@RequestBody String data) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.queryConfig(config);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按key查询配置
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/getconfigbykey", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByKey(@RequestBody String data) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.queryConfigByKey(config.getCkey());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 获取ios版本
     * @param response
     * @return
     */
    @RequestMapping(value = "/version/ios", method = {RequestMethod.GET})
    @ResponseBody
    public String queryVersionByIos(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        ResultModel resultModel = configServices.queryConfigByKey("APP_VERSION_IOS");
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 获取android 版本
     * @param response
     * @return
     */
    @RequestMapping(value = "/version/android", method = {RequestMethod.GET})
    @ResponseBody
    public String queryVersionByAndroid(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        ResultModel resultModel = configServices.queryConfigByKey("APP_VERSION_ANDROID");
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * luci 版本
     * @param response
     * @return
     */
    @RequestMapping(value = "/version/luci", method = {RequestMethod.GET})
    @ResponseBody
    public String queryVersionByLuci(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        ResultModel resultModel = configServices.queryConfigByKey("LUCI_VERSION");
        return new JsonResult().SerailzeObject(resultModel.getObject());
    }


    /**
     * 修改配置信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/upd", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String doupd(@RequestBody String data) {
        Config config = JsonStringUtil.readValue(data, Config.class);
        ResultModel resultModel = configServices.doUpd(config);
        return new JsonResult().Serailze(resultModel);
    }


}
